package club.itstudio.itouc.controller.admin

import club.itstudio.itouc.config.ElectiveCoursecConfig
import club.itstudio.itouc.config.LessonConfig
import club.itstudio.itouc.core.annotation.AdminRequired
import club.itstudio.itouc.core.util.Result
import club.itstudio.itouc.core.util.ResultUtil
import club.itstudio.itouc.core.util.mapper
import club.itstudio.itouc.util.Redis
import com.fasterxml.jackson.module.kotlin.readValue
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/admin/config")
class ConfigController (val ecConfig: ElectiveCoursecConfig,
                        val lessonConfig: LessonConfig,
                        val redis: Redis){

    /**
     * 获取配置信息
     */
    @AdminRequired
    @GetMapping
    fun getConfig(): Result<Any>{
        val configMap = mutableMapOf<String, Any>()

        val ecConfigStr = redis.getEcConfigStr()
        if (ecConfigStr.isEmpty()) {
            configMap["ecConfig"] = ElectiveCoursecConfig()
        }else {
            configMap["ecConfig"] = mapper.readValue<ElectiveCoursecConfig>(ecConfigStr)
        }

        val lessonConfigStr = redis.getLessonConfigStr()
        if (lessonConfigStr.isEmpty()) {
            configMap["lessonConfig"] = LessonConfig()
        }else {
            configMap["lessonConfig"] = mapper.readValue<LessonConfig>(redis.getLessonConfigStr())
        }
        return ResultUtil.success(configMap)
    }


    /**
     * 更新选课配置
     */
    @AdminRequired
    @PostMapping("/ecConfig")
    fun updateEcConfig(@RequestParam year: String,
                       @RequestParam semester: String,
                       @RequestParam open: Boolean,
                       @RequestParam refresh: Boolean): Result<Any>  {
        val config = ElectiveCoursecConfig(year, semester, open, refresh)
        ecConfig.year = config.year
        ecConfig.semester = config.semester
        ecConfig.open = config.open
        ecConfig.refresh = config.refresh
        ecConfig.timestamp = config.timestamp
        redis.saveEcConfig(config)
        return ResultUtil.success()
    }

    /**
     * 更新课程信息配置中的学年、学期、学期起始时间
     */
    @AdminRequired
    @PostMapping("/lesson/date")
    fun updateLessonConfig(@RequestParam startTime: String,
                           @RequestParam year: String,
                           @RequestParam semester: String): Result<Any> {
        val dateArr = startTime.split("-")
        lessonConfig.startDate[0] = dateArr[0].toInt()
        lessonConfig.startDate[1] = dateArr[1].toInt()
        lessonConfig.startDate[2] = dateArr[2].toInt()
        lessonConfig.year = year
        lessonConfig.semester = semester
        lessonConfig.timestamp = System.currentTimeMillis()
        redis.saveLessonConfig(lessonConfig)
        return ResultUtil.success()
    }

    /**
     * 添加放假日期
     */
    @AdminRequired
    @PostMapping("/lesson/holiday")
    fun addHoliday(@RequestParam date: String): Result<Any> {
        lessonConfig.dateHolidaySet.add(date)
        lessonConfig.timestamp = System.currentTimeMillis()
        redis.saveLessonConfig(lessonConfig)
        return ResultUtil.success()
    }

    /**
     * 删除放假日期
     */
    @AdminRequired
    @DeleteMapping("/lesson/holiday")
    fun deleteHoliday(@RequestParam date: String): Result<Any> {
        lessonConfig.dateHolidaySet.remove(date)
        lessonConfig.timestamp = System.currentTimeMillis()
        redis.saveLessonConfig(lessonConfig)
        return ResultUtil.success()
    }

    /**
     * 添加替换日期
     * @param oldDate 旧日期
     * @param newDate 新日期
     * 旧日期的课程让新日期代替
     */
    @AdminRequired
    @PostMapping("/lesson/replace")
    fun addReplaceDate(@RequestParam oldDate: String,
                       @RequestParam newDate: String): Result<Any> {
        lessonConfig.dateReplaceMap[oldDate] = newDate
        lessonConfig.timestamp = System.currentTimeMillis()
        redis.saveLessonConfig(lessonConfig)
        return ResultUtil.success()
    }

    /**
     * 删除替换日期
     */
    @AdminRequired
    @DeleteMapping("/lesson/replace")
    fun deleteReplaceDate(@RequestParam oldDate: String): Result<Any> {
        lessonConfig.dateReplaceMap.remove(oldDate)
        lessonConfig.timestamp = System.currentTimeMillis()
        redis.saveLessonConfig(lessonConfig)
        return ResultUtil.success()
    }

}